package com.hzhr.iot.statis.convertor.custom;

import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.statis.Global;
import com.hzhr.iot.statis.constant.DeviceOptions;
import com.hzhr.iot.statis.convertor.DeviceDailyGasConsumeConvertor;
import com.hzhr.iot.statis.entity.ext.DeviceCollectDeviceEntityExt;
import com.hzhr.iot.statis.entity.ext.DeviceDailyGasConsumeEntityExt;
import com.hzhr.iot.statis.entity.ext.DeviceIotDeviceEntityExt;
import com.hzhr.iot.statis.entity.ext.OptionsEntityExt;
import com.hzhr.iot.statis.model.deviceDailyGasConsume.DeviceDailyGasConsumeModel;
import com.hzhr.iot.statis.model.deviceDataStatisExceptionReport.DeviceDataStatisExceptionReportModel;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class DeviceDailyGasConsumeCustomConvertor extends DeviceDailyGasConsumeConvertor {
    @Autowired
    DeviceOptions deviceOptions;
    @Autowired
    User_DeptService user_deptService;
    @Autowired
    Global global;

    @Override
    public DeviceDailyGasConsumeModel ext2deviceDailyGasConsumeModel(DeviceDailyGasConsumeEntityExt in) {
        DeviceDailyGasConsumeModel result = super.ext2deviceDailyGasConsumeModel(in);

        DeviceCollectDeviceEntityExt ext1 = in.getCollectDeviceExt();
        if (ext1 != null) {
            result.setCollectDeviceCategoryCode(ext1.getCategoryCode());
            result.setCollectDeviceResourceCode(ext1.getResourceCode());
            result.setGasUserCode(ext1.getGasUserCode());
        }

        return result;
    }

    @Override
    public DeviceDataStatisExceptionReportModel ext2deviceDataStatisExceptionReportModel(DeviceDailyGasConsumeEntityExt in) {
        DeviceDataStatisExceptionReportModel result = super.ext2deviceDataStatisExceptionReportModel(in);

        DeviceCollectDeviceEntityExt ext1 = in.getCollectDeviceExt();
        if (ext1 != null) {
            result.setCollectDeviceCode(ext1.getCode());
            result.setGasUserCode(ext1.getGasUserCode());
            result.setGasUserName(ext1.getGasUserName());
            result.setGasUserAddress(ext1.getGasUserAddress());

            String categoryCode = ext1.getCategoryCode();
            if (!StringUtils.isEmpty(categoryCode)) {
                try {
                    result.setCollectDeviceCategoryDes(deviceOptions.getDesByCode("COLLECT_DEVICE_CATEGORY", categoryCode));
                } catch (ServiceException e) {

                }
            }

            Integer deptId = ext1.getDeptId();
            if (deptId != null) {
                try {
                    Map<String, String> header = new HashMap<>();
                    header.put("token", global.getFirstToken());
                    Map<String, Object> query = new HashMap<>();
                    query.put("_show", "one");
                    query.put("_equal_id", deptId);
                    query.put("_show_managementName", true);
                    query.put("_show_subCompanyName", true);

                    DeptModel deptModel = user_deptService.get(header, query).getModel();
                    if (deptModel != null) {
                        result.setManagementName(deptModel.getManagementName());
                        result.setSubCompanyName(deptModel.getSubCompanyName());
                    }
                } catch (ServiceException e) {

                }

            }
        }

        DeviceIotDeviceEntityExt ext2 = in.getIotDeviceExt();
        if (ext2 != null) {
            result.setIotDeviceCode(ext2.getCode());
            String categoryCode = ext2.getCategoryCode();
            if (!StringUtils.isEmpty(categoryCode)) {
                try {
                    result.setIotDeviceCategoryDes(deviceOptions.getDesByCode("IOT_DEVICE_CATEGORY", categoryCode));
                } catch (ServiceException e) {

                }
            }
        }

        OptionsEntityExt ext3 = in.getDataStatusExt();
        if (ext3 != null) {
            result.setExceptionTypeDes(ext3.getDes());
        }

        return result;
    }
}

